package com.woniuxy.oa.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.oa.entity.Approval;
import com.woniuxy.oa.entity.Employee;
import com.woniuxy.oa.entity.Goods_claim;
import com.woniuxy.oa.entity.Goods_purchase;
import com.woniuxy.oa.entity.Holiday;
import com.woniuxy.oa.entity.PageBean;
import com.woniuxy.oa.entity.Reimbursement;
import com.woniuxy.oa.enums.ApprovalApplyType;
import com.woniuxy.oa.exception.ApprovalException;
import com.woniuxy.oa.mapper.ApprovalMapper;
import com.woniuxy.oa.service.ApprovalService;
import com.woniuxy.oa.utils.ApprovalUtil;

@Service
public class ApprovalServiceImpl implements ApprovalService {

	@Autowired
	ApprovalMapper approvalMapper;

	public void setApprovalMapper(ApprovalMapper approvalMapper) {
		this.approvalMapper = approvalMapper;
	}

	@Override
	public List<Employee> getApprovalPersonsById(Integer id) {
		try {
			// 根据当前用户id找到部门号
			Integer did = approvalMapper.selectDidById(id);
			List<Employee> ApprovalPersons = approvalMapper.selectApprovalPersonByDid(did);
			return ApprovalPersons;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApprovalException(e);
		}
	}

	@Override
	public Integer addApprovalGetId(Approval approval) {
		try {
			approvalMapper.insertApprovalGetId(approval);
			Integer getId = approval.getA_id();
			if (getId > 0) {
				return getId;
			} else {
				throw new ApprovalException("插入失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApprovalException(e);
		}
	}

	@Override
	public PageBean<Approval> getApprovalPageBean(Integer pageIndex, Integer pageSize, String name, Approval approval,
			Holiday holiday, Reimbursement reimbursement) {
		try {
			PageBean<Approval> pb = new PageBean<Approval>();
			// 查询总记录条数,根据发起人名字、审批类型、审批状态、申请时间,和类型条件
			// 已测试，能用
			int totalRecord = approvalMapper.getApprovalPageBeanCount(name, approval,holiday, reimbursement);
			// 设置总记录条数
			pb.setTotalRecord(totalRecord);
			// 设置每页显示条数
			pb.setPageSize(pageSize);
			// 总页数，
			pb.setTotalPage(pb.getTotalPage());
			pb.setPageIndex(pageIndex);
			pb.setPageBeginAndPageEnd();

			// 查询符合条件的所有记录,按发起人名字 审批类型，审批状态、申请 时间，请假条件，报销类型
			// 使用分步查询,
			// 根据approval的atype属性，决定直接查询 或者 先得到list,解析每个type,再去相应表中组装
			// 已测试，能够使用
			List<Approval> beanList = null;
			if(approval.getA_type()==null){
				//如果类型为空,先根据审批人查出审批
				beanList = approvalMapper.selectApprovalPageBeanByConditionAndPageIndex(
						(pageIndex - 1) * pageSize, pageSize, name, approval, holiday, reimbursement);
				for (int i = 0; i < beanList.size(); i++) {
					//遍历，取到类型id
					Integer type = beanList.get(i).getA_type();
					Integer a_id = beanList.get(i).getA_id();
					
					//为这个approval读出额外表属性，需要审批号、类型
					if(type==1) {
						//加班
						beanList.get(i).setOverTime(approvalMapper.selectOverTimeByAid(a_id));
					}
					if(type==2) {
						//请假
						
					
						beanList.get(i).setHoliday(approvalMapper.selectHolidayByAidWhenTypeIsEmpty(a_id));
				
					}
					if(type==3) {
						//报销
						beanList.get(i).setReimbursement(approvalMapper.selectReimbursementByAidWhenTypeIsEmpty(a_id));
					}
					if(type==4) {
						//申领
						beanList.get(i).setGoodsClaim(approvalMapper.selectGoodClaimByAid(a_id));

					}
					if(type==5) {
						//申购
						beanList.get(i).setGoodsPurchase(approvalMapper.selectGoodPurchaseByAid(a_id));

					}
					if(type==6) {
						//通知
						beanList.get(i).setNotice(approvalMapper.selectNoticeByAid(a_id));

					}
					
				}
				
			} else {
				//不为空则直接传
				beanList = approvalMapper.selectApprovalPageBeanByConditionAndPageIndex(
						(pageIndex - 1) * pageSize, pageSize, name, approval, holiday, reimbursement);
			}
			pb.setBeanList(beanList);
			
			return pb;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApprovalException(e);
		}
	}

	@Override
	public void addRelationship(Integer table_key, Integer a_id, Integer a_type) {
		try {
			//插入关系
			approvalMapper.insertRelationship(table_key,a_id,a_type);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApprovalException(e);
		}
		
	}

	//根据审批号查审批
	@Override
	public Approval getApprovalByAid(Integer a_id) {
		try {
			return approvalMapper.selectApprovalByAid(a_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApprovalException(e);
		}
	}

	//审批方法
	@Override
	public void examine(Integer a_id, String flag) {
		try {
			if("agree".equals(flag)) {
				approvalMapper.agree(a_id);
			}
			if("disagree".equals(flag)) {
				approvalMapper.disagree(a_id);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApprovalException(e);
		}
	}

	//根据审批号查申领
	@Override
	public Goods_claim getGcidByAid(Integer a_id) {
		try {
			return approvalMapper.selectGoodClaimByAid(a_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApprovalException(e);
		}
	}

	@Override
	public Goods_purchase getGpidByAid(Integer a_id) {
		try {
			return approvalMapper.selectGoodPurchaseByAid(a_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApprovalException(e);
		}
	}

	//向审批表写入审批时间
	@Override
	public void writeExamineTime(Integer a_id) {
		try {
			approvalMapper.writeExamineTime(a_id,ApprovalUtil.getNow());
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApprovalException(e);
		}
	}


}
